Public Class frmComputerGroupPolicies
    Public policy As policyType
    Private Sub radComputerGroupPoliciesScheduledReminders_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radComputerGroupPoliciesScheduledReminders.CheckedChanged
        With Me
            .chkComputerGroupPoliciesScheduledRemindersTime1.Enabled = True
            .chkComputerGroupPoliciesScheduledRemindersTime2.Enabled = True
            .chkComputerGroupPoliciesScheduledRemindersTime3.Enabled = True

            If .chkComputerGroupPoliciesScheduledRemindersTime1.Checked Then
                .numComputerGroupPoliciesScheduledRemindersTimeHour1.Enabled = True
                .numComputerGroupPoliciesScheduledRemindersTimeMinute1.Enabled = True
                .cmbComputerGroupPoliciesScheduledRemindersTimeAMPM1.Enabled = True
            End If

            If .chkComputerGroupPoliciesScheduledRemindersTime2.Checked Then
                .numComputerGroupPoliciesScheduledRemindersTimeHour2.Enabled = True
                .numComputerGroupPoliciesScheduledRemindersTimeMinute2.Enabled = True
                .cmbComputerGroupPoliciesScheduledRemindersTimeAMPM2.Enabled = True
            End If

            If .chkComputerGroupPoliciesScheduledRemindersTime3.Checked Then
                .numComputerGroupPoliciesScheduledRemindersTimeHour3.Enabled = True
                .numComputerGroupPoliciesScheduledRemindersTimeMinute3.Enabled = True
                .cmbComputerGroupPoliciesScheduledRemindersTimeAMPM3.Enabled = True
            End If

            .lblPeriodicRemindersText1.Enabled = False
            .numComputerGroupPoliciesPeriodicRemindersInterval.Enabled = False
            .lblPeriodicRemindersText2.Enabled = False
        End With
    End Sub

    Private Sub radComputerGroupPoliciesPeriodReminders_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radComputerGroupPoliciesPeriodReminders.CheckedChanged
        With Me
            .chkComputerGroupPoliciesScheduledRemindersTime1.Enabled = False
            .chkComputerGroupPoliciesScheduledRemindersTime2.Enabled = False
            .chkComputerGroupPoliciesScheduledRemindersTime3.Enabled = False
            .numComputerGroupPoliciesScheduledRemindersTimeHour1.Enabled = False
            .numComputerGroupPoliciesScheduledRemindersTimeHour2.Enabled = False
            .numComputerGroupPoliciesScheduledRemindersTimeHour3.Enabled = False
            .numComputerGroupPoliciesScheduledRemindersTimeMinute1.Enabled = False
            .numComputerGroupPoliciesScheduledRemindersTimeMinute2.Enabled = False
            .numComputerGroupPoliciesScheduledRemindersTimeMinute3.Enabled = False
            .cmbComputerGroupPoliciesScheduledRemindersTimeAMPM1.Enabled = False
            .cmbComputerGroupPoliciesScheduledRemindersTimeAMPM2.Enabled = False
            .cmbComputerGroupPoliciesScheduledRemindersTimeAMPM3.Enabled = False

            .lblPeriodicRemindersText1.Enabled = True
            .numComputerGroupPoliciesPeriodicRemindersInterval.Enabled = True
            .lblPeriodicRemindersText2.Enabled = True
        End With
    End Sub

    Private Sub chkComputerGroupPoliciesScheduledRemindersTime1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkComputerGroupPoliciesScheduledRemindersTime1.CheckedChanged
        numComputerGroupPoliciesScheduledRemindersTimeHour1.Enabled = chkComputerGroupPoliciesScheduledRemindersTime1.Checked
        numComputerGroupPoliciesScheduledRemindersTimeMinute1.Enabled = chkComputerGroupPoliciesScheduledRemindersTime1.Checked
        cmbComputerGroupPoliciesScheduledRemindersTimeAMPM1.Enabled = chkComputerGroupPoliciesScheduledRemindersTime1.Checked
    End Sub

    Private Sub chkComputerGroupPoliciesScheduledRemindersTime2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkComputerGroupPoliciesScheduledRemindersTime2.CheckedChanged
        numComputerGroupPoliciesScheduledRemindersTimeHour2.Enabled = chkComputerGroupPoliciesScheduledRemindersTime2.Checked
        numComputerGroupPoliciesScheduledRemindersTimeMinute2.Enabled = chkComputerGroupPoliciesScheduledRemindersTime2.Checked
        cmbComputerGroupPoliciesScheduledRemindersTimeAMPM2.Enabled = chkComputerGroupPoliciesScheduledRemindersTime2.Checked
    End Sub

    Private Sub chkComputerGroupPoliciesScheduledRemindersTime3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkComputerGroupPoliciesScheduledRemindersTime3.CheckedChanged
        numComputerGroupPoliciesScheduledRemindersTimeHour3.Enabled = chkComputerGroupPoliciesScheduledRemindersTime3.Checked
        numComputerGroupPoliciesScheduledRemindersTimeMinute3.Enabled = chkComputerGroupPoliciesScheduledRemindersTime3.Checked
        cmbComputerGroupPoliciesScheduledRemindersTimeAMPM3.Enabled = chkComputerGroupPoliciesScheduledRemindersTime3.Checked
    End Sub

    Private Sub btnComputerGroupPoliciesOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputerGroupPoliciesOK.Click
        Dim msg As protocol
        Dim tempDate As Date
        Dim tempUse As Boolean

        policy.checkInTime = (60 * Me.numComputerGroupPoliciesCheckinTimeHours.Value) + Me.numComputerGroupPoliciesCheckinTimeMinutes.Value
        If Me.numComputerGroupPoliciesScheduledRemindersTimeHour1.Value = 0 Then Me.numComputerGroupPoliciesScheduledRemindersTimeHour1.Value = 12
        policy.rebootPolicy.time1 = Date.Parse("7/5/2007 " & Me.numComputerGroupPoliciesScheduledRemindersTimeHour1.Value & ":" & Me.numComputerGroupPoliciesScheduledRemindersTimeMinute1.Value & Me.cmbComputerGroupPoliciesScheduledRemindersTimeAMPM1.SelectedItem.ToString)
        policy.rebootPolicy.useTime1 = Me.chkComputerGroupPoliciesScheduledRemindersTime1.Checked
        If Me.numComputerGroupPoliciesScheduledRemindersTimeHour2.Value = 0 Then Me.numComputerGroupPoliciesScheduledRemindersTimeHour2.Value = 12
        policy.rebootPolicy.time2 = Date.Parse("7/5/2007 " & Me.numComputerGroupPoliciesScheduledRemindersTimeHour2.Value & ":" & Me.numComputerGroupPoliciesScheduledRemindersTimeMinute2.Value & Me.cmbComputerGroupPoliciesScheduledRemindersTimeAMPM2.SelectedItem.ToString)
        policy.rebootPolicy.useTime2 = Me.chkComputerGroupPoliciesScheduledRemindersTime2.Checked
        If Me.numComputerGroupPoliciesScheduledRemindersTimeHour3.Value = 0 Then Me.numComputerGroupPoliciesScheduledRemindersTimeHour3.Value = 12
        policy.rebootPolicy.time3 = Date.Parse("7/5/2007 " & Me.numComputerGroupPoliciesScheduledRemindersTimeHour3.Value & ":" & Me.numComputerGroupPoliciesScheduledRemindersTimeMinute3.Value & Me.cmbComputerGroupPoliciesScheduledRemindersTimeAMPM3.SelectedItem.ToString)
        policy.rebootPolicy.useTime3 = Me.chkComputerGroupPoliciesScheduledRemindersTime3.Checked
        policy.rebootPolicy.everyFewMinutes = Me.numComputerGroupPoliciesPeriodicRemindersInterval.Value
        If Me.radComputerGroupPoliciesScheduledReminders.Checked Then policy.rebootPolicy.rebootSchedule = rebootScheduleType.threeTimesADay Else policy.rebootPolicy.rebootSchedule = rebootScheduleType.everyFewMinutes

        If policy.rebootPolicy.time1 > policy.rebootPolicy.time2 Then
            tempDate = policy.rebootPolicy.time1
            tempUse = policy.rebootPolicy.useTime1
            policy.rebootPolicy.time1 = policy.rebootPolicy.time2
            policy.rebootPolicy.useTime1 = policy.rebootPolicy.useTime2
            policy.rebootPolicy.time2 = tempDate
            policy.rebootPolicy.useTime2 = tempUse
        End If
        If policy.rebootPolicy.time2 > policy.rebootPolicy.time3 Then
            tempDate = policy.rebootPolicy.time2
            tempUse = policy.rebootPolicy.useTime2
            policy.rebootPolicy.time2 = policy.rebootPolicy.time3
            policy.rebootPolicy.useTime2 = policy.rebootPolicy.useTime3
            policy.rebootPolicy.time3 = tempDate
            policy.rebootPolicy.useTime3 = tempUse
        End If
        If policy.rebootPolicy.time1 > policy.rebootPolicy.time2 Then
            tempDate = policy.rebootPolicy.time1
            tempUse = policy.rebootPolicy.useTime1
            policy.rebootPolicy.time1 = policy.rebootPolicy.time2
            policy.rebootPolicy.useTime1 = policy.rebootPolicy.useTime2
            policy.rebootPolicy.time2 = tempDate
            policy.rebootPolicy.useTime2 = tempUse
        End If
        If policy.rebootPolicy.time2 > policy.rebootPolicy.time3 Then
            tempDate = policy.rebootPolicy.time2
            tempUse = policy.rebootPolicy.useTime2
            policy.rebootPolicy.time2 = policy.rebootPolicy.time3
            policy.rebootPolicy.useTime2 = policy.rebootPolicy.useTime3
            policy.rebootPolicy.time3 = tempDate
            policy.rebootPolicy.useTime3 = tempUse
        End If

        policy.rebootPolicy.scheduledRebootDay = Me.cmbComputerGroupPoliciesScheduledRebootDay.SelectedIndex

        With policy.auPolicy
            .enabled = Me.chkComputerGroupPoliciesEnableAU.Checked
            .day = Me.cmbComputerGroupPoliciesAUDay.SelectedIndex
            .time = Me.cmbComputerGroupPoliciesAUHour.SelectedItem.ToString & Me.cmbComputerGroupPoliciesAUMinute.SelectedItem.ToString & Me.cmbComputerGroupPoliciesAUampm.SelectedItem.ToString
        End With

        msg.type = protocolType.A_setPolicy
        With policy
            msg.message = .id
            msg.message = msg.message & "::" & .name
            msg.message = msg.message & "::" & .checkInTime
            msg.message = msg.message & "::" & .rebootPolicy.everyFewMinutes
            msg.message = msg.message & "::" & .rebootPolicy.time1
            msg.message = msg.message & "::" & .rebootPolicy.useTime1
            msg.message = msg.message & "::" & .rebootPolicy.time2
            msg.message = msg.message & "::" & .rebootPolicy.useTime2
            msg.message = msg.message & "::" & .rebootPolicy.time3
            msg.message = msg.message & "::" & .rebootPolicy.useTime3
            msg.message = msg.message & "::" & .rebootPolicy.rebootSchedule
            msg.message = msg.message & "::" & .rebootPolicy.scheduledRebootDay
            msg.message = msg.message & "::" & .auPolicy.enabled
            msg.message = msg.message & "::" & .auPolicy.day
            msg.message = msg.message & "::" & .auPolicy.time
        End With

        frmMain.net.sendMessage(msg)
        frmMain.net.getMessage(msg)
        policy.id = msg.message
        'frmMain.treeComputers.SelectedNode.Tag = policy.id
        msg.message = policy.id & "::" & frmMain.treeComputers.SelectedNode.Tag
        msg.type = protocolType.A_applyPolicyToGroup
        frmMain.net.sendMessage(msg)
        frmMain.net.getMessage(msg)

        If msg.type = protocolType.SA_ERROR Then
            MsgBox("Error saving policy.  Try again later.")
        Else
            Me.Close()
        End If
    End Sub

    Private Sub chkComputerGroupPoliciesEnableAU_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkComputerGroupPoliciesEnableAU.CheckedChanged
        Me.cmbComputerGroupPoliciesAUDay.Enabled = Me.chkComputerGroupPoliciesEnableAU.Checked
        Me.cmbComputerGroupPoliciesAUHour.Enabled = Me.chkComputerGroupPoliciesEnableAU.Checked
        Me.cmbComputerGroupPoliciesAUampm.Enabled = Me.chkComputerGroupPoliciesEnableAU.Checked
        Me.cmbComputerGroupPoliciesAUMinute.Enabled = Me.chkComputerGroupPoliciesEnableAU.Checked

        If chkComputerGroupPoliciesEnableAU.Checked Then
            If Me.cmbComputerGroupPoliciesAUDay.SelectedIndex = -1 Then Me.cmbComputerGroupPoliciesAUDay.SelectedIndex = 0
            If Me.cmbComputerGroupPoliciesAUHour.SelectedIndex = -1 Then Me.cmbComputerGroupPoliciesAUHour.SelectedIndex = 0
            If Me.cmbComputerGroupPoliciesAUampm.SelectedIndex = -1 Then Me.cmbComputerGroupPoliciesAUampm.SelectedIndex = 0
            If Me.cmbComputerGroupPoliciesAUMinute.SelectedIndex = -1 Then Me.cmbComputerGroupPoliciesAUMinute.SelectedIndex = 0
        End If
    End Sub
End Class